/**
 * Created by user on 2015/10/14.
 */

angular.module('mis.h5Manager.controllers', [])
    .controller('h5Ctrl', ['$scope','$http', '$location', function ($scope, $http, $location) {

        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/h5/h5List/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.h5List = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);

            },
            error: function(err){
                //$scope.communityList = null;
            },
            params: {}
        });

        //全选列表
        $scope.allSelected = function () {
            $scope.pagination.allSelected();
        };

        $scope.addData = {};
        $scope.add = function(addData){
            if(!addData.name){alert("请输入H5名称！");return;}
            if(!addData.url){alert("请输入H5的Url！");return;}
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/h5/h5Add',
                addData: addData,
                success: function(data){
                    $scope.pagination.selectPage(1);//插入成功显示第一页
                    $('#doc-modal-add').modal("close");
                    if(data.result.status == 1){
                        alert("新增成功！");
                        $scope.addData = {};
                    }else{
                        alert("新增失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-add').modal("close");
                    alert("新增失败！");
                }
            };
            $scope.pagination.listAdd(params);
        };

        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/h5/h5Delete',
                checkObj: $(".item-check"),//checkbox对象
                key: "id",
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                    alert("删除成功！");

                },
                error: function(){}
            };
            $scope.pagination.listDelete(params);
        };


        //搜索
        $scope.search = function (btn) {

            var opt = {};
            //if (countyId) body.countyId = countyId;
            if ($scope.name) opt.name = $scope.name;
            if ($scope.desc) opt.name = $scope.desc;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/h5/h5List/' + 1 + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.h5List = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = opt;
            }).error(function (err) {
                alert(err);
            });

        };

    }]);